<style>
    .order-edit-btn { margin: 0 auto; display: block; margin-top: 25px; }
    .coreshop-form { overflow-y: auto; height: 100%; box-sizing: border-box; }
    .select-address { display: inline-block !important; width: 120px; margin-right: 10px; padding: 10px 0px; border: 1px solid #e6e6e6; margin-bottom: 0px; }
</style>
<script type="text/html" template lay-done="layui.data.sendParams(d);">
    <div class="layui-form coreshop-form layui-form-pane" lay-filter="LAY-app-CoreCmsOrder-shipForm" id="LAY-app-CoreCmsOrder-shipForm">
        <input type="hidden" name="orderId" value="{{d.params.data.orderModel.orderId}}">
        <div class="layui-form-item">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-inline layui-inline-100 layui-form-mid">
                {{d.params.data.orderModel.orderId}}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">配送方式</label>
            <div class="layui-input-inline layui-inline-4 layui-form-mid">
                {{d.params.data.orderModel.logisticsName || ''}}
            </div>
            <label class="layui-form-label">配送费</label>
            <div class="layui-input-inline layui-inline-4 layui-form-mid">
                {{d.params.data.orderModel.costFreight}}
            </div>
            <label class="layui-form-label">商品总重量</label>
            <div class="layui-input-inline layui-inline-4 layui-form-mid">
                {{d.params.data.orderModel.weight}}
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">买家备注</label>
            <div class="layui-input-inline layui-inline-100 layui-form-mid">
                {{d.params.data.orderModel.memo || '无'}}
            </div>
        </div>
        {{# if (d.params.data.orderModel.storeId==0) { }}
        <div class="layui-form-item">
            <label class="layui-form-label">收货人姓名</label>
            <div class="layui-input-inline layui-inline-4">
                <input type="text" name="shipName" value="{{d.params.data.orderModel.shipName}}" placeholder="收货人" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">收货人电话</label>
            <div class="layui-input-inline layui-inline-4">
                <input type="text" name="shipMobile" value="{{d.params.data.orderModel.shipMobile}}" placeholder="电话" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">收货地址</label>
            <div class="layui-input-block address-class">
                <input type="hidden" name="shipAreaId" value="{{d.params.data.orderModel.shipAreaId}}" />
                <input type="text" name="shipAddress" value="{{d.params.data.orderModel.shipAddress}}" placeholder="详细地址" autocomplete="off" class="layui-input" style="width:400px;display:inline;">
            </div>
        </div>
        {{# }else{ }}
        <div class="layui-form-item">
            <label class="layui-form-label">收货门店</label>
            <div class="layui-input-block">
                <select name="storeId">
                    {{# layui.each(d.params.data.storeList, function(index, item){ }}
                    <option value="{{item.id}}" {{item.id==d.params.data.orderModel.storeId?'selected="selected"':''}}>{{item.storeName}} {{item.mobile}} {{item.allAddress}}</option>
                    {{# }); }}
                </select>
            </div>
        </div>
        {{# } }}


        <div class="layui-form-item" style="margin-bottom:10px;">
            <table class="layui-table" lay-size="sm">
                <colgroup>
                    <col>
                    <col>
                    <col>
                    <col width="70">
                    <col width="70">
                    <col width="70">
                    <col width="70">
                </colgroup>
                <thead>
                <tr>
                    <th>商品名称</th>
                    <th>商品编码</th>
                    <th>货品编码</th>
                    <th>购买数量</th>
                    <th>已退数量</th>
                    <th>已发数量</th>
                    <th>发货数量</th>
                </tr>
                </thead>
                {{# layui.each(d.params.data.orderModel.items, function(index, item){ }}
                <tr>
                    <td>{{item.name}} - {{item.addon}}</td>
                    <td>{{item.bn}}</td>
                    <td>{{item.sn}}</td>
                    <td>{{item.nums}}</td>
                    <td>{{item.reshipNums}}</td>
                    <td>{{item.sendNums}}</td>
                    <td>
                        <input type="hidden" name="items[{{index}}]" value="{{item.productId}}" />
                        <input type="number" data-id="{{item.productId}}" name="itemsValue[{{index}}]" class="layui-input order-ship-nums no-margin-bottom" max="{{item.nums-item.sendNums-item.reshipNums+item.reshipedNums}}" value="{{item.nums-item.sendNums-item.reshipNums+item.reshipedNums}}" min="0">
                    </td>
                </tr>
                {{# }); }}
            </table>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">物流公司</label>
                <div class="layui-input-inline layui-inline-4">
                    <select name="logiCode" lay-verify="required" lay-reqText="请选择物流公司" lay-search>
                        <option value="">搜索选择物流公司</option>
                        {{# layui.each(d.params.data.logistics, function(index, item){ }}
                        <option value="{{item.logiCode}}">{{item.logiName}}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">直播物流</label>
                <div class="layui-input-inline layui-inline-4">
                    <select name="deliveryCompanyId" lay-verify="required" lay-reqText="直播物流" lay-search>
                        <option value="">搜索选择物流公司</option>
                        <option value="OTHERS" selected="selected">无物流</option>
                        {{# layui.each(d.params.data.deliveryCompany, function(index, item){ }}
                        <option value="{{item.deliveryId}}">{{item.deliveryName}}</option>
                        {{# }); }}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">物流单号</label>
                <div class="layui-input-inline layui-inline-3">
                    <input type="text" name="logiNo" value="" class="layui-input" lay-verType="tips" lay-verify="required">
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">发货备注</label>
            <div class="layui-input-block">
                <textarea name="memo" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item core-hidden">
            <input type="button" class="layui-btn" lay-submit lay-filter="LAY-app-CoreCmsOrder-shipForm-submit" id="LAY-app-CoreCmsOrder-shipForm-submit" value="确认发货">
        </div>
    </div>
</script>
<script>
    var debug = layui.setter.debug;
    layui.data.sendParams = function (d) {
        //开启调试情况下获取接口赋值数据
        if (debug) { console.log(d.params.data); }
        layui.use(['admin', 'form', 'laydate', 'upload', 'coreHelper', 'cropperImg'],
            function () {
                var $ = layui.$
                    , form = layui.form
                    , admin = layui.admin
                    , laydate = layui.laydate
                    , upload = layui.upload
                    , cropperImg = layui.cropperImg
                    , coreHelper = layui.coreHelper;

                coreHelper.Post("Api/Tools/GetAreaCheckedList", { id: d.params.data.orderModel.shipAreaId }, function (data) {
                    if (debug) { console.log(data); } //开启调试返回数据
                    var str = "";
                    $.each(data, function (i, n) {
                        str += "<select lay-ignore name='areaId_" + (i + 1) + "' dep='" + (i + 1) + "'  class='select-address'>";
                        str += "<option value='' >请选择</option>";
                        $.each(n.list, function (h, z) {
                            if (n.hasOwnProperty('info') && n.info.id == z.id) {
                                str += "<option value='" + z.id + "' selected='selected'>" + z.name + "</option>";
                            } else {
                                str += "<option value='" + z.id + "' >" + z.name + "</option>";
                            }
                        });
                        str += "</select>";

                    });
                    $("input[name='shipAreaId']").after(str);
                    //以上数据输出完，以下绑定事件
                    $.each(data, function (i, n) {
                        if (i < (data.length)) {
                            $("select[name='areaId_" + (i + 1) + "']").change(function () {
                                changeareaIdArea(i + 1, data.length);
                            });
                        }
                    });

                });
                function changeareaIdArea(i, max_i) {
                    //清除后面节点
                    for (var x = i + 1; x <= 6; x++) {  //最多6层，足够了
                        $("select[name='areaId_" + x + "']").remove();
                    }
                    var val = $("select[name='areaId_" + i + "']").val();
                    if (val != "") {
                        //取子节点数据，然后显示下一级
                        coreHelper.Post("Api/Tools/GetAreaChildren", { id: val }, function (e) {
                            if (debug) { console.log(e); } //开启调试返回数据

                            if (e.length > 0) {
                                var str = "";
                                str += "<select lay-ignore name='areaId_" + (i + 1) + "' dep='" + (i + 1) + "'  class='select-address'>";
                                str += "<option value='' >请选择</option>";
                                $.each(e, function (h, z) {
                                    str += "<option value='" + z.id + "' >" + z.name + "</option>";
                                });
                                str += "</select>";
                                $("select[name='areaId_" + i + "']").after(str);
                                //以上数据输出完，以下绑定事件
                                $("select[name='areaId_" + (i + 1) + "']").change(function () {
                                    changeareaIdArea(i + 1, i + 2);
                                });

                                //如果有返回值，就说明省市区没有选择到最终节点
                                if (1 == 1) {
                                    $("input[name='shipAreaId']").val("");
                                } else {
                                    $("input[name='shipAreaId']").val($("select[name='areaId_" + i + "']").val());
                                }
                            } else {
                                $("input[name='shipAreaId']").val($("select[name='areaId_" + i + "']").val());
                            }
                        });
                    } else {
                        if (1 == 1) {
                            $("input[name='shipAreaId']").val("");
                        } else {
                            //第一级的元素就直接赋值为空就是了
                            if (i == 1) {
                                $("input[name='shipAreaId']").val("");
                            } else {
                                i--;
                                $("input[name='shipAreaId']").val($("select[name='areaId_" + i + "']").val());
                            }

                        }
                    }
                }

                //重载form
                form.render(null, 'LAY-app-CoreCmsOrder-shipForm');
            })
    };
</script>